package org.mat.ipaas.ucenter.bff.admin.modules.system;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import org.mat.framework.lang.dto.MatApiResponse;
import org.mat.ipaas.ucenter.service.api.system.SysAnnouncementRestAPI;
import org.mat.ipaas.ucenter.service.dto.system.entity.SysAnnouncementDTO;
import org.mat.ipaas.ucenter.service.dto.system.entity.SysAnnouncementSendDTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;

/**
 * @Title: Controller
 * 系统通告表
 */
@RestController
@RequestMapping("/sys/annountCement")
@Slf4j
public class SysAnnouncementController {

    @Autowired
    private SysAnnouncementRestAPI sysAnnouncementRestAPI;

    /**
     * 分页列表查询
     *
     * @param sysAnnouncementDTO
     * @param pageNo
     * @param pageSize
     * @param req
     * @return
     */
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public MatApiResponse<Page<SysAnnouncementDTO>> queryPageList(SysAnnouncementDTO sysAnnouncementDTO,
                                                                   @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                                                   @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                                                   HttpServletRequest req) {
        MatApiResponse<Page<SysAnnouncementDTO>> matApiResponse = sysAnnouncementRestAPI.queryPageList(sysAnnouncementDTO, pageNo, pageSize);
        return matApiResponse;
    }

    /**
     * 添加
     *
     * @param sysAnnouncementDTO
     * @return
     */
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public MatApiResponse<SysAnnouncementDTO> add(@RequestBody SysAnnouncementDTO sysAnnouncementDTO) {
        MatApiResponse<SysAnnouncementDTO> matApiResponse = sysAnnouncementRestAPI.add(sysAnnouncementDTO);
        return matApiResponse;
    }

    /**
     * 编辑
     *  TODO edit拼错了，改一下
     *
     * @param sysAnnouncementDTO
     * @return
     */
    @RequestMapping(value = "/edit", method = {RequestMethod.PUT, RequestMethod.POST})
    public MatApiResponse<SysAnnouncementDTO> eidt(@RequestBody SysAnnouncementDTO sysAnnouncementDTO) {
        MatApiResponse<SysAnnouncementDTO> matApiResponse = sysAnnouncementRestAPI.eidt(sysAnnouncementDTO);
        return matApiResponse;
    }

    /**
     * 通过id删除
     *
     * @param id
     * @return
     */
    @RequestMapping(value = "/delete", method = RequestMethod.DELETE)
    public MatApiResponse<SysAnnouncementDTO> delete(@RequestParam(name = "id", required = true) String id) {
        MatApiResponse<SysAnnouncementDTO> matApiResponse = sysAnnouncementRestAPI.delete(id);
        return matApiResponse;
    }

    /**
     * 批量删除
     *
     * @param ids
     * @return
     */
    @RequestMapping(value = "/deleteBatch", method = RequestMethod.DELETE)
    public MatApiResponse<SysAnnouncementDTO> deleteBatch(@RequestParam(name = "ids", required = true) String ids) {
        MatApiResponse<SysAnnouncementDTO> matApiResponse = sysAnnouncementRestAPI.deleteBatch(ids);
        return matApiResponse;
    }

    /**
     * 通过id查询
     *
     * @param id
     * @return
     */
    @RequestMapping(value = "/queryById", method = RequestMethod.GET)
    public MatApiResponse<SysAnnouncementDTO> queryById(@RequestParam(name = "id", required = true) String id) {
        MatApiResponse<SysAnnouncementDTO> matApiResponse = sysAnnouncementRestAPI.queryById(id);
        return matApiResponse;
    }

    /**
     * 更新发布操作
     *
     * @param id
     * @return
     */
    @RequestMapping(value = "/doReleaseData", method = RequestMethod.GET)
    public MatApiResponse<SysAnnouncementDTO> doReleaseData(@RequestParam(name = "id", required = true) String id, HttpServletRequest request) {
        String accessToken = request.getHeader("X-Access-Token");
        MatApiResponse<SysAnnouncementDTO> matApiResponse = sysAnnouncementRestAPI.doReleaseData(accessToken, id);
        return matApiResponse;
    }

    /**
     * 更新撤销操作
     *
     * @param id
     * @return
     */
    @RequestMapping(value = "/doReovkeData", method = RequestMethod.GET)
    public MatApiResponse<SysAnnouncementDTO> doReovkeData(@RequestParam(name = "id", required = true) String id, HttpServletRequest request) {
        MatApiResponse<SysAnnouncementDTO> matApiResponse = sysAnnouncementRestAPI.doReovkeData(id);
        return matApiResponse;
    }

    /**
     * @return
     * @功能：补充用户数据，并返回系统消息
     */
    @RequestMapping(value = "/listByUser", method = RequestMethod.GET)
    public MatApiResponse<Map<String, Object>> listByUser(@RequestParam(required = false, defaultValue = "5") Integer pageSize) {
        MatApiResponse<Map<String, Object>> matApiResponse = sysAnnouncementRestAPI.listByUser(pageSize);
        return matApiResponse;
    }


    /**
     * 导出excel
     * TODO 导出逻辑要不要透传到service？考虑bff层完成组装
     */
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(SysAnnouncementDTO sysAnnouncementDTO) {
        ModelAndView modelAndView = sysAnnouncementRestAPI.exportXls(sysAnnouncementDTO);
        return modelAndView;
    }

    /**
     * 通过excel导入数据
     * TODO 导入逻辑要不要透传到service？考虑bff层完成组装
     *
     * @return
     */
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public MatApiResponse<?> importExcel() {
        MatApiResponse<?> matApiResponse = sysAnnouncementRestAPI.importExcel();
        return matApiResponse;
    }

    /**
     * 同步消息
     *
     * @param anntId
     * @return
     */
    @RequestMapping(value = "/syncNotic", method = RequestMethod.GET)
    public MatApiResponse<SysAnnouncementDTO> syncNotic(@RequestParam(name = "anntId", required = false) String anntId) {
        MatApiResponse<SysAnnouncementDTO> matApiResponse = sysAnnouncementRestAPI.syncNotic(anntId);
        return matApiResponse;
    }

    /**
     * 通告查看详情页面（用于第三方APP）
     *
     * @param modelAndView
     * @param id
     * @return
     */
    @GetMapping("/show/{id}")
    public ModelAndView showContent(ModelAndView modelAndView, @PathVariable("id") String id) {
        ModelAndView modelAndViewResponse = sysAnnouncementRestAPI.showContent(modelAndView, id);
        return modelAndViewResponse;
    }

    /**
     * 【vue3用】 消息列表查询
     *
     * @param fromUser
     * @param beginDate
     * @param endDate
     * @param pageNo
     * @param pageSize
     * @return
     */
    @RequestMapping(value = "/vue3List", method = RequestMethod.GET)
    public MatApiResponse<List<SysAnnouncementDTO>> vue3List(@RequestParam(name = "fromUser", required = false) String fromUser,
                                                             @RequestParam(name = "starFlag", required = false) String starFlag,
                                                             @RequestParam(name = "rangeDateKey", required = false) String rangeDateKey,
                                                             @RequestParam(name = "beginDate", required = false) String beginDate, @RequestParam(name = "endDate", required = false) String endDate,
                                                             @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo, @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize) {
        MatApiResponse<List<SysAnnouncementDTO>> matApiResponse = sysAnnouncementRestAPI.vue3List(fromUser, starFlag, rangeDateKey, beginDate, endDate, pageNo, pageSize);
        return matApiResponse;
    }


    /**
     * 根据用户id获取最新一条消息发送时间(创建时间)
     *
     * @param userId
     * @return
     */
    @GetMapping("/getLastAnnountTime")
    public MatApiResponse<Page<SysAnnouncementSendDTO>> getLastAnnountTime(@RequestParam(name = "userId") String userId) {
        MatApiResponse<Page<SysAnnouncementSendDTO>> matApiResponse = sysAnnouncementRestAPI.getLastAnnountTime(userId);
        return matApiResponse;
    }
}
